//
//  ST_AICenterKnighthoodCell.swift
//  btvideo
//
//  Created by Jason on 2021/8/30.
//  Copyright © 2021 JarvanZhang. All rights reserved.
//

import UIKit
import SwiftyJSON


class ST_AICenterKnighthoodCell: UICollectionViewCell {

    static let height = 570.cgFloat
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        contentView.addSubview(knighthood_background_view)
        knighthood_background_view.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(15)
            make.right.equalToSuperview().offset(-15)
            make.top.equalToSuperview()
            make.height.equalTo(94)
        }
        
        
        knighthood_background_view.addSubview(icon_image_view)
        icon_image_view.snp.makeConstraints { (make) in
            make.width.height.equalTo(50)
            make.left.equalToSuperview().offset(20)
            make.centerY.equalToSuperview()
        }
        
        
        knighthood_background_view.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.top.equalTo(icon_image_view)
            make.left.equalTo(icon_image_view.snp.right).offset(10)
            make.height.equalTo(25)
        }
        
        knighthood_background_view.addSubview(content_label)
        content_label.snp.makeConstraints { (make) in
            make.top.equalTo(titleLabel.snp.bottom).offset(2)
            make.left.equalTo(titleLabel)
            make.height.equalTo(17)
        }
        
        knighthood_background_view.addSubview(click_label)
        click_label.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-35)
            make.height.equalTo(32)
            make.width.equalTo(100)
        }
        
        contentView.addSubview(member_label)
        
        member_label.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(knighthood_background_view.snp.bottom).offset(20)
            make.height.equalTo(21)
        }
        
        var lastVerticalView:ST_AICenterFaceStatusCellChildBox?
        
        for (idx,item) in ST_AICenterFaceStatusCellChildBoxModel.setup_knighthood_data().enumerated(){
            
            let rowIndex = idx % 2
            
            let itemView = ST_AICenterFaceStatusCellChildBox()
            
//            itemView.backgroundColor = .random
            
            itemView.model = item
            contentView.addSubview(itemView)
            
            if rowIndex == 0{
                itemView.snp.makeConstraints { (make) in
                    make.size.equalTo(ST_AICenterFaceStatusCellChildBox.size)
                    make.right.equalTo(contentView.snp.centerX).offset(-7)
                    
                    if let last = lastVerticalView{
                        make.top.equalTo(last.snp.bottom).offset(15)
                    }else{
                        make.top.equalTo(member_label.snp.bottom).offset(15)
                    }
                }
            }else{
                
                itemView.snp.makeConstraints { (make) in
                    make.size.equalTo(ST_AICenterFaceStatusCellChildBox.size)
                    make.left.equalTo(contentView.snp.centerX).offset(8)
                    if let last = lastVerticalView{
                        make.top.equalTo(last)
                    }else{
                        make.top.equalTo(member_label.snp.bottom).offset(15)
                    }
                }
            }
            if rowIndex == 0{
                lastVerticalView = itemView
            }
        }
        
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
//    MARK: - Properties
    fileprivate lazy var knighthood_background_view:UIButton = {
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setBackgroundImage(ST_ImageUtils.imageName("Knighthood_background_icon"), for: UIControl.State.normal)
//        control.add_touch_up(insideTarget: self, action: #selector(push_handle))
        control.isUserInteractionEnabled = false
        return control
    }()
    

    fileprivate lazy var icon_image_view:UIImageView = {
         let control = UIImageView()
          control.contentMode = .scaleAspectFill
          control.cornerRadius = 22
          return control
      }()

    
    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        control.textColor = UIColor(hex: 0xEAC377)
        control.font = UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.semibold)
        return control
    }()
    
    
    fileprivate lazy var content_label:UILabel = {
        let control = UILabel.init()
        control.textColor = UIColor(hex: 0xEAC377)
        control.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.regular)
        return control
    }()
    
    
    fileprivate lazy var click_label:UILabel = {
        let control = UILabel.init()
        control.cornerRadius = 16
        control.text = "我的贵族"
        control.textAlignment = .center
        control.backgroundColor = #colorLiteral(red: 0.9176470588, green: 0.7647058824, blue: 0.4666666667, alpha: 1)
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.semibold)
        control.textColor = UIColor.white
        return control
    }()
    
    fileprivate lazy var member_label:UILabel = {
        
        let control = UILabel.init()
        
        let attrString = NSMutableAttributedString(string: "贵族特权")
        
        control.numberOfLines = 0
        
        let attr: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 15, weight: UIFont.Weight.semibold),.foregroundColor: UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 1)]
        
        attrString.addAttributes(attr, range: NSRange(location: 0, length: attrString.length))
        
        control.attributedText = attrString
        
        return control
    }()
    
    var json = JSON.null{
        didSet{
            refreshStatus()
        }
    }
}

extension ST_AICenterKnighthoodCell{
    
    @objc func push_handle()  {
        
        
    }
    
}

extension ST_AICenterKnighthoodCell{
    
    @objc func refreshStatus(){
                
        icon_image_view.pin_setImage(from: json["image"].stringValue.compress_small_image(width: 120).url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
        
        titleLabel.text = json["name"].stringValue
        
        content_label.text = json["desc"].stringValue
        
    }
    
}
